﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using Jy123.Common;
using System.Collections.Generic;

namespace Jy123.Web.Module.Club
{
    public class SaveProps : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string strUserId = Cookie.getCookie("UserId");
            if (!string.IsNullOrEmpty(strUserId))
            {
                int userId = int.Parse(strUserId);
                string[] strProps = context.Request["Props"].Split(',');
                string type = context.Request["Type"];
                if (string.IsNullOrEmpty(type))
                {
                    type = "Star";
                }
                if (type.Equals("Car"))
                {
                    BLL.Club_FittingsRecord BLLFRecord = new Jy123.BLL.Club_FittingsRecord();
                    string strClubId = context.Request["ClubId"];
                    string strFWhere = string.Format("FClub = {0}", strClubId);
                    List<Model.Club_FittingsRecord> listFRecord = BLLFRecord.GetModelList(strFWhere);

                    foreach (Model.Club_FittingsRecord record in listFRecord)
                    {
                        if (IsInList(record, strProps))
                        {
                            record.IsUsed = true;
                        }
                        else
                        {
                            record.IsUsed = false;
                        }
                        BLLFRecord.Update(record);
                    }
                }
                else
                {
                    BLL.Club_PropsRecord BLLRecord = new Jy123.BLL.Club_PropsRecord();
                    string strWhere = string.Format("FUser = {0}", strUserId);
                    List<Model.Club_PropsRecord> listRecord = BLLRecord.GetModelList(strWhere);

                    foreach (Model.Club_PropsRecord record in listRecord)
                    {
                        if (IsInList(record, strProps))
                        {
                            record.IsUsed = true;
                        }
                        else
                        {
                            record.IsUsed = false;
                        }
                        BLLRecord.Update(record);
                    }
                }

            }
            else
            {
                context.Response.Redirect("/");
            }
        }

        private bool IsInList(Model.Club_FittingsRecord record, string[] strProps)
        {
            if (strProps.Length > 0)
            {
                for (int i = 0; i < strProps.Length; i++)
                {
                    if (!string.IsNullOrEmpty(strProps[i]))
                    {
                        int cId = int.Parse(strProps[i]);
                        if (record.FFittings == cId)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        private bool IsInList(Model.Club_PropsRecord record, string[] strProps)
        {
            if (strProps.Length > 0)
            {
                for (int i = 0; i < strProps.Length; i++)
                {
                    if (!string.IsNullOrEmpty(strProps[i]))
                    {
                        int cId = int.Parse(strProps[i]);
                        if (record.FProps == cId)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
